package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.EnterShopDto;
import com.scpii.universal.hibernate.dao.EnterShopManageDao;
import com.scpii.universal.pns.dao.TagsDao;
import com.scpii.universal.service.EnterShopManageService;

/**
 * 
 * universalWeb
 * 
 * @author zhangqiang 2012-7-10下午05:21:48
 */
@Service
public class EnterShopManageServiceImpl extends
		BaseServiceImpl<EnterShopDto, Integer> implements
		EnterShopManageService {
	@Resource
	private EnterShopManageDao enterShopManageDao;
	@Resource
	private TagsDao tagsDao;

	@Resource
	public void setBaseDao(EnterShopManageDao enterShopManageDao) {
		super.setBaseDao(enterShopManageDao);
	}

	@Override
	public Integer getCountByCondition(EnterShopDto condition,String from) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.EnterShopDto(t1.intShopId)");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		hql.append(" from EnterpShopTab t1 where");
		hql.append(" t1.intAppId=:intAppId ");
		argsMap.put("intAppId", condition.getIntAppId());
		if("c".equals(from)){
			hql.append(" and t1.intCateId=:intCateId");
			argsMap.put("intCateId", condition.getIntCateId());
		} else {
			hql.append(" and t1.intCateId=0");
		}
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");

		// 查询条件
		if (!StringUtils.isBlank(condition.getVarShopName()) && !("请输入门店名称或地址").equals(condition.getVarShopName())) {
			hql.append(" and (t1.varShopName like:varShopName");
			hql.append(" or t1.varShopAddress like:varShopName)");
			argsMap.put("varShopName", "%"+condition.getVarShopName()+"%");
		}
		if(condition.getVarShopProvince() !=null && !"省份".equals(condition.getVarShopProvince())) {
			hql.append(" and t1.varShopProvince=:varShopProvince");
			argsMap.put("varShopProvince",condition.getVarShopProvince());
			hql.append(" and t1.varShopCity=:varShopCity");
			argsMap.put("varShopCity",condition.getVarShopCity());
			hql.append(" and t1.varShopArea=:varShopArea");
			argsMap.put("varShopArea",condition.getVarShopArea());
		}
		if(!StringUtils.isBlank(condition.getTags())) {
			hql.append(" and exists(select 1 from TagValues t4 where t4.targetId="+GenericConstants.CATE_ID_PRE+"+t1.intShopId");
			hql.append(" and t4.tagId in("+condition.getTags().substring(1)+"))");
		}
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<EnterShopDto> list = enterShopManageDao.hqlGetList(hql.toString(),
				argsMap);
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public List<EnterShopDto> getEnterShopList(EnterShopDto condition,String from,
			Integer pageNo, Integer pageSize) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.EnterShopDto(t1.intShopId,");
		hql.append(" t1.intAppId,");
		hql.append(" t1.intEnterpId,");
		hql.append(" t1.varShopName,");
		hql.append(" t1.varShopCountry,");
		hql.append(" t1.varShopProvince,");
		hql.append(" t1.varShopCity,");
		hql.append(" t1.varShopLongitude,  ");
		hql.append(" t1.varShopLatitude , ");
		hql.append(" t1.varShopPostnum , ");
		hql.append(" t1.varShopPhonenum , ");
		hql.append(" t1.varShopAddress , ");
		hql.append(" t2.varEnterpName , ");
		hql.append(" t3.varAppName ,");
		hql.append(" t1.intContentId , ");
		hql.append(" t1.intCateId , ");
		hql.append(" t1.varShopArea,");
		hql.append(" t1.varIconUrl) ");
		hql.append(" from EnterpShopTab t1 , ");
		hql.append("  EnterpriseTab t2, ");
		hql.append("  AppTab t3 ");
		hql.append(" where t1.intEnterpId= t2.intEnterpId ");
		hql.append(" and t1.intAppId= t3.intAppId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign ");

		Map<String, Object> argsMap = new HashMap<String, Object>();

		// 查询条件
		if (condition != null) {
			hql.append(" and t1.intEnterpId =:intEnterpId");
			argsMap.put("intEnterpId", condition.getIntEnterpId());
			hql.append(" and t1.intAppId=:intAppId ");
			argsMap.put("intAppId", condition.getIntAppId());
			if("c".equals(from)){
				hql.append(" and t1.intCateId=:intCateId");
				argsMap.put("intCateId", condition.getIntCateId());
			} else {
				hql.append(" and t1.intCateId=0");
			}
			if (!StringUtils.isBlank(condition.getVarShopName()) && !("请输入门店名称或地址").equals(condition.getVarShopName())) {
				hql.append(" and (t1.varShopName like:varShopName");
				hql.append(" or t1.varShopAddress like:varShopName)");
				argsMap.put("varShopName", "%"+condition.getVarShopName()+"%");
			}
			if(condition.getVarShopProvince() !=null && !"省份".equals(condition.getVarShopProvince())) {
				hql.append(" and t1.varShopProvince=:varShopProvince");
				argsMap.put("varShopProvince",condition.getVarShopProvince());
				hql.append(" and t1.varShopCity=:varShopCity");
				argsMap.put("varShopCity",condition.getVarShopCity());
				hql.append(" and t1.varShopArea=:varShopArea");
				argsMap.put("varShopArea",condition.getVarShopArea());
			}
			if(!StringUtils.isBlank(condition.getTags())) {
				hql.append(" and exists(select 1 from TagValues t4 where t4.targetId="+GenericConstants.CATE_ID_PRE+"+t1.intShopId");
				hql.append(" and t4.tagId="+condition.getTags()+"))");
			}
			//如果内容ID不为0，那么表示门店与内容相关
			if (condition.getIntContentId()!=null) {
				hql.append(" and t1.intContentId=:intContentId ");
				argsMap.put("intContentId", condition.getIntContentId());
			}
		}

		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		hql.append(" order by t1.datUpdateDatetime desc");

		List<EnterShopDto> result = enterShopManageDao.findByHql(hql.toString(), pageNo, pageSize, argsMap);
		if("c".equals(from)){
			//封装tags
			StringBuffer sb = new StringBuffer();
			if(result!=null && result.size() >0){
				for(EnterShopDto enterShopDto:result){
					sb.append(",");
					sb.append(GenericConstants.CATE_ID_PRE+enterShopDto.getIntShopId());
				}
				Map<String,String> tagNames = tagsDao.getTagNamesByTargetId(sb.substring(1).toString());
				String targetId = null;
				for(EnterShopDto enterShopDto:result){
					targetId = String.valueOf(GenericConstants.CATE_ID_PRE+enterShopDto.getIntShopId());
					if(tagNames.containsKey(targetId)){
						enterShopDto.setTags(tagNames.get(targetId));
					}
				}
			}
		}
		return result;
	}
}
